class Solution {
public:
    string sortSentence(string s) {
        
        vector<string>save;
        int cur=0;
        string tmp;

        while(cur<=s.size())//将字符串入数组
        {
            if(s[cur]==' '||cur==s.size())
            {
                save.push_back(tmp);
                tmp.clear();
                cur++;
            }
            else{
                tmp.push_back(s[cur++]);
            }
        }

        int index=0;
        while(index<save.size())//调整字符所在的位置
        {
            int number=save[index].back()-48;
            if(number-1==index)
            {
                index++;
            }
            else {
                swap(save[index],save[number-1]);
            }
        }


        tmp.clear();
        index=0;
        while(index<save.size())//插入到串中
        {
           tmp+=save[index++];
           tmp.back()=' ';
        }


        tmp.pop_back();//删除多余的空格
        return tmp;
    }
};
